<?php
/**
 * upload controller
 * @package app-file_controller
 */
load_controller('/file');
/**
 * upload controller
 * @author pandajingjing
 * @package app-file_controller
 */
class uploadcontroller extends filecontroller{

	function doRequest(){
		load_lib('/bll/savefile');
		$clsBll=new bll_savefile();
		$arrUpdFiles=$arrErrMsg=array();
		$bolErrFnd=false;
		$mixResult=$clsBll->suitDomain('upd',$this->getParam('HTTP_HOST','server'));
		if(true===$mixResult){
			$strFromURL=$this->getParam('HTTP_REFERER','server');
			$mixResult=$clsBll->chkAllowedDomain($strFromURL);
			if(true===$mixResult){
				$arrFiles=$this->uploadMulti();
				$strIP=$this->getClientIP();
				foreach($arrFiles as $arrFile){
					$arrUpdFile=array();
					$mixResult=$clsBll->saveFile($arrFile['name'],$arrFile['type'],$arrFile['tmp_name'],$arrFile['error'],$arrFile['size'],$strIP,$arrUpdFile);
					if(true===$mixResult){
						$arrUpdFiles[$arrFile['key']]=$arrUpdFile;
					}else{
						$arrUpdFiles[$arrFile['key']]=$mixResult;
					}
				}
			}else{
				$bolErrFnd=true;
				$arrErrMsg=array( 
						$mixResult 
				);
			}
		}else{
			$bolErrFnd=true;
			$arrErrMsg=array( 
					$mixResult 
			);
		}
		
		$this->setData('jsonErrMsg',json_encode($arrErrMsg));
		$this->setData('bolErrFnd',$bolErrFnd);
		$this->setData('jsonUpdFiles',json_encode($arrUpdFiles));
		return '/upload';
	}

	/**
	 * 获取所有上传的文件信息
	 * @return array
	 */
	protected function uploadMulti(){
		$arrFiles=$this->getParams('file');
		$arrMulti=array();
		foreach($arrFiles as $strKey=>$mixFiles){
			if(is_array($mixFiles['name'])){
				$intCnt=count($mixFiles['name']);
				for($i=0;$i<$intCnt;++$i){
					$arrMulti[]=array( 
							'key'=>$strKey.'_'.$i,
							'name'=>$mixFiles['name'][$i],
							'type'=>$mixFiles['type'][$i],
							'tmp_name'=>$mixFiles['tmp_name'][$i],
							'error'=>$mixFiles['error'][$i],
							'size'=>$mixFiles['size'][$i] 
					);
				}
			}else{
				$arrMulti[]=array( 
						'key'=>$strKey,
						'name'=>$mixFiles['name'],
						'type'=>$mixFiles['type'],
						'tmp_name'=>$mixFiles['tmp_name'],
						'error'=>$mixFiles['error'],
						'size'=>$mixFiles['size'] 
				);
			}
		}
		return $arrMulti;
	}
}